/*
 * @Author: jingfuu@163.com
 * @Date: 2024-12-16 09:47:39
 * @LastEditTime: 2024-12-16 14:36:35
 * @Description: file content
 */
import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Query,
} from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import ExamDistrictService from '../service/ExamDistrictService';
import ExamDistrictQueryParam from '../param/ExamDistrictQueryParam';
import ExamDistrictVo from '../vo/ExamDistrictVo';
import { R } from 'src/common/R';
import { Log } from 'src/common/decorator/LogDecorator';
import ExamDistrictDto from '../dto/ExamDistrictDto';
import BusinessType from 'src/common/enum/BusinessType';

@ApiTags('考点接口')
@Controller('/exam/district')
class ExamDistrictController {
  constructor(private readonly examDistrictService: ExamDistrictService) {}

  @ApiOperation({ summary: '获取考点列表' })
  @Get('list')
  async findExamDistrictList(
    @Query() param: ExamDistrictQueryParam,
  ): Promise<R<ExamDistrictVo>> {
    return R.page(await this.examDistrictService.findExamDistrictList(param));
  }

  @ApiOperation({ summary: '获取考点详情' })
  @Get(':id(\\d+)')
  async findExamDistrictDetail(
    @Param('id') id: number,
  ): Promise<R<ExamDistrictVo>> {
    return R.ok(await this.examDistrictService.detail(id));
  }

  @ApiOperation({ summary: '新增考点' })
  @Log({
    title: '考点',
    businessType: BusinessType.INSERT,
  })
  @Post()
  async createExamDistrict(
    @Body() examDistrict: ExamDistrictDto,
  ): Promise<R<void>> {
    await this.examDistrictService.createExamDistrict(examDistrict);
    return R.ok();
  }

  @ApiOperation({ summary: '修改考点' })
  @Log({
    title: '考点',
    businessType: BusinessType.UPDATE,
  })
  @Put()
  async modifyExamDistrict(
    @Body() examDistrict: ExamDistrictDto,
  ): Promise<R<void>> {
    await this.examDistrictService.modifyExamDistrict(examDistrict);
    return R.ok();
  }

  @ApiOperation({ summary: '删除考点' })
  @Log({
    title: '考点',
    businessType: BusinessType.DELETE,
  })
  @Delete('/:id')
  async deleteExamDistrict(@Param('id') id: string): Promise<R<any>> {
    if (id.indexOf(',') > -1) {
      for (const item of id.split(',')) {
        await this.examDistrictService.deleteExamDistrict(
          Number.parseInt(item),
        );
      }
    } else {
      await this.examDistrictService.deleteExamDistrict(Number.parseInt(id));
    }
    return R.ok();
  }
}
export default ExamDistrictController;
